package com.xjtu.platform.admin.dao;

import com.xjtu.platform.admin.dto.*;
import com.xjtu.platform.admin.entity.ReaderBaseInfo;
import com.xjtu.platform.common.entity.FileEntity;
import com.xjtu.platform.common.entity.PrimaryKey;
import com.xjtu.platform.reader.dto.GetBookListParamDto;
import com.xjtu.platform.reader.entity.Author;
import com.xjtu.platform.reader.entity.BookBaseInfo;
import com.xjtu.platform.reader.entity.BookLabel;
import com.xjtu.platform.reader.entity.PenaltyEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @program: platform
 * @description: 新增读者
 * @author: Wushanxu
 * @create: 2023-11-15 23:32
 **/
@Mapper
public interface AddReaderDao {
    @Insert("insert into reader " +
            "VALUES(null,#{dto.name},#{dto.readerCode},#{dto.sex},#{dto.phone},#{dto.age},#{dto.idenId},#{dto.pwd})")
    @Options(useGeneratedKeys = true, keyColumn = "reader_id", keyProperty = "pk.id")
    void addNewReader(@Param("dto") AddReaderDto addReaderDto, @Param("pk") PrimaryKey primaryKey);

    @Delete("DELETE FROM reader WHERE reader_id = #{dto.readerId};")
    void deleteReader(@Param("dto") DeleteReaderDto deleteReaderDto);

    @Select("select * from reader " +
            "where name like #{dto.name} " +
            "and reader_code like #{dto.reader_code} " +
            "and sex like #{dto.sex} " +
            "and age like #{dto.age} " +
            "and phone like #{dto.phone} " +
            "and iden_id like #{dto.iden_id}" +
            "order by reader_id ASC")
    List<ReaderBaseInfo> getReaderBaseInfoByParam(@Param("dto") GetReaderListParamDto dto);

    @Update("UPDATE reader " +
            "SET reader_code = #{dto.reader_code}, " +
            "    name = #{dto.name}, " +
            "    phone = #{dto.phone}, sex = #{dto.sex}, age = #{dto.age}, pwd = #{dto.pwd}, iden_id = #{dto.iden_id}" +
            "WHERE reader_id = #{dto.reader_id};")
    void modifyBook(@Param("dto") EditReaderDto editReaderDto, @Param("pk") PrimaryKey primaryKey);

    @Select("select * from book " +
            "where book_name like #{dto.book_name} " +
            "and ISBN like #{dto.isbn} and " +
            "book_id in (SELECT book_id from author_book,author where author_name like #{dto.author_name} and author.author_id = author_book.author_id) and \n" +
            "book_id in (SELECT book_id from book_class,class where class_name like #{dto.class_name} and class.class_id = book_class.class_id) and \n" +
            "book_id in (SELECT book_id from book_lable,library_label where label_name like #{dto.label_name} and book_lable.label_id = library_label.label_id) " +
            "order by book_id ASC")
    List<BookBaseInfo> getBookBaseInfoByParam(@Param("dto") GetBookListDto dto);

    @Select("select class_name from book_class,class where book_class.class_id = class.class_id and book_id = #{bookId}")
    String getBookClass(String bookId);

    @Select("select file.file_id,file_name,file_address as file_url from file,book_pic " +
            "where book_pic.file_id = file.file_id " +
            "and book_pic.book_id = #{bookId} " +
            "order by file.file_id ASC")
    List<FileEntity> getBookPic(String bookId);


    @Select("select author.author_id,author_name,author_info " +
            "from author,author_book " +
            "where author_book.author_id = author.author_id " +
            "and author_book.book_id = #{bookId}")
    List<Author> getBookAuthors(String bookId);

    @Select("select library_label.label_id, label_name " +
            "from library_label,book_lable " +
            "where book_lable.label_id = library_label.label_id " +
            "and book_lable.book_id = #{bookId}")
    List<BookLabel> getBookLabelList(String bookId);

    @Select("select * from book " +
            "where book_name like #{dto.book_name} " +
            "and ISBN like #{dto.isbn} and current_num = 0 and " +
            "book_id in (SELECT book_id from author_book,author where author_name like #{dto.author_name} and author.author_id = author_book.author_id) and \n" +
            "book_id in (SELECT book_id from book_class,class where class_name like #{dto.class_name} and class.class_id = book_class.class_id) and \n" +
            "book_id in (SELECT book_id from book_lable,library_label where label_name like #{dto.label_name} and book_lable.label_id = library_label.label_id) " +
            "order by book_id ASC")
    List<BookBaseInfo> getEmptyBookBaseInfoByParam(@Param("dto") GetBookListDto dto);
}
